<? require "../../../global.php"; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require_once('./lib/chinabank.MotoClient.php');
require_once('./lib/userfunction.php');

// 消费
$motoClient = new MotoClient();
$cardid     =trim($_POST["cardid"]);       // 信用卡卡号
$month     =trim($_POST["month"]);       // 月
$year     =trim($_POST["year"]);       // 年
$year_month = $year.$month;
$cvv     =trim($_POST["cvv"]);       // 年
$money     =trim($_POST["money"]);       // 金额 单位元
$name     =trim($_POST["name"]);       // 持卡人姓名
$idcard     =trim($_POST["idcard"]);       // 证件号
$type1    =trim($_POST["type1"]);	//支付款项类型	
$orderid=trim($_POST[ "orderid" ]);

if($idcard=="")
{
	$idcard="999999";
}


//初始化支付信息,当支付列表里没有支付信息时添加一条新的支付记录

preg_match("/^[a-zA-Z][0-9]+/",$orderid,$orderAr);
$orderidl=$orderAr[0];
$payflag=str_replace($orderidl,"",$orderid);

require RootDir."/inc/dabase_mysql.php";
$mydb=new YYBDB();
$sql="select orderno from ".$SystemConest[7]."orderpaylist where orderno='".$orderidl."'";
$res=$mydb->db_query($sql);
if($mydb->db_num_rows($res)<1)
{
	$sql="insert into ".$SystemConest[7]."orderpaylist (tourid,orderno,paytype,earnbizhong) value(".$_REQUEST['productid'].",'$orderidl','chinabank','RMB')";
	$mydb->db_query($sql);
}


$result = $motoClient->consume( $_POST[ "orderid" ], $cardid, $year_month, $money*100, array("name"=>$name, "idcard"=>$idcard, "cvv2"=>$cvv, 'mobile'=>'139','type1'=>$type1, 'note'=>'nothing'));

//echo $result[$motoClient->MOTO_SEARCH_AMOUNT] . "<br />";
//print_a("RESULT Array", $result);
//echo "oid: {$result[$motoClient->MOTO_KEY_OID]},<br/> authcode: {$result[$motoClient->MOTO_KEY_AUTHID]},<br/>";
echo "result: {".formatalt($result[result])."}<br/>";

if($result[result]==0 and !strstr($result[result],"E"))//当返回0时交易流程成功，但要调用查询才能知道是否交易成功
{
	
	//当返回0时，表示是实时的，可以调用查询马上得到结果
	/**
	result：0可以成功查询到此交易,$rsearch[$motoClient->MOTO_SEARCH_STATECODE]==0支付成功
	*/
	$rsearch = $motoClient->search($orderid);
	if($rsearch[result]==0 and $rsearch[$motoClient->MOTO_SEARCH_STATECODE]==0)
	{
		
	
		//更改数据库里的订单状态
		require RootDir."/inc/dabase_mysql.php";
		$mydb=new YYBDB();

			
		$sql2="earnestmoney=".$money.",paytimeearnestmoney=".time().",paytype='chinabank',earnbizhong='RMB',isconfim=1";
		$paytype="dj";
			if($payflag!="")
			{
				if($payflag=="je2")
				{
					$sql2=",balance=".$money.",paytimebalance=".time().",balancepaytye='chinabank',balancebizhong='RMB',balanceisconfim=1";
					$paytype="je2";
				}
				if($payflag=="je3")
				{
					$sql2=",je3=".$money.",je3paytime=".time().",je3paytype='chinabank',je3bizhong='RMB',je3isconfim=1";
					$paytype="je3";
				}
				if($payflag=="je4")
				{
					$sql2=",je4=".$money.",je4paytime=".time().",je4paytype='chinabank',je4bizhong='RMB',je4isconfim=1";
					$paytype="je4";
				}
				
			}
			$sql="update ".$SystemConest[7]."orderpaylist set 
			".$sql2."
			  where orderno='".$orderidl."'";
			 $mydb->db_query($sql);

			 $res=$mydb->db_query("select * from ".$SystemConest[7]."tourorder where orderno='".$orderidl."'");
			 $rs=$mydb->db_fetch_array($res);
			 //支付成功，执行发送邮件操作
			 echo "<iframe  src='/".Q."SendMail_sendMailInfo_type_paysuss_orderno_".$orderidl."_paytype_".$paytype."_email_".$rs["nuseremail"]."' 
frameborder=0 scrolling=no width=1 height=1></iframe>";
			 unset($rs);
			 
			require RootDir."/inc/Uifunction.php";
			$url="/".Q."paytype_showpaysucctocustomer_orderno_".$orderidl."_moneynum_".$money."_bizhong_RMB_paytype_chinabank_djorwk_$paytype";
			gourl($url,1);
			
	}

}
elseif($result[result]==1)
{
	echo "<br>您的卡是非实时交易卡，请稍后请进入<a href='http://usa.dreams-travel.com/user/dsztourorder_searchone'>订单查询页面查询</a><br>";
}
else
{
	//支付失败时
	echo "你的信用卡不能在本系统支付,或者你的信用卡设置了密码,或者卡号不对,所以不能支付!<br>请换一张信用卡或者换一种支付方式!";
}


?>
